﻿//17:24 2010-2-10  D4E840CD9F8C4DEF6FB6CA4A30754ED8
var firefive =
{
	onLoad: function()
	{
		// 初始化
		this.initialized = true;
		this.strings = document.getElementById("firefive-strings");
		
		
	},

	load: function()
	{
		document.getElementById("urlbar").addEventListener("keypress", function (event) { firefive.url_key_press(event); }, false);
		document.getElementById("urlbar").addEventListener("focus", function () { firefive.url_get_focus(); }, false);
	},

	url_get_focus : function ()
	{
		// 地址栏获得焦点
	},

	with_url_update : function ()
	{
		alert("左侧按钮被点击");
		window.openDialog("chrome://firefive/content/update_url.xul", "update", "chrome,dialog,centerscreen,titlebar,alwaysraised,modal");
		
	},

	update : function ()
	{
		alert(content.document.location.toString());
		
		url_shortener.shorten(content.document.location.toString());
		
		document.getElementById("firefive-update").src = "chrome://firefive/skin/waiting.gif";
		
		var text = document.getElementById("urlbar").value;
		var xml = new XMLHttpRequest();
		var update_url = 'http://api.follow5.com/api/statuses/update.json?api_key=D766BE2C290AC55BB9397C7AB410A906'

		xml.open("POST",update_url,false);
		var auth = "Basic " + Base64.encode('firefive:asdfghjk');
		xml.setRequestHeader('Authorization', auth);
		xml.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

		xml.onreadystatechange =  function (aEvt) {
			if (xml.readyState == 4) {
				if(xml.status == 200)
					alert(xml.responseText);
				else
					alert("Error loading page\n");
			}
		};
		
		xml.send('status='+text);
		var response_text = xml.responseText;
		if (response_text == "{}")
			alert("有重复的～!");
		else
			alert("发送成功～!");

		document.getElementById("firefive-update").src = "chrome://firefive/skin/follow5_mouseon.png";
		
	},
/*
json 解析
alert(xml.status + xml.statusText);
if(xml.status == 200){
var str = xml.responseText;
alert(str);
var obj = eval('(' + str + ')');   
alert(obj[0].id);

} */
/***************************************************
	open_follow5: function()
	{
		var tabbrowser = gBrowser;
		var tabs = tabbrowser.tabContainer.childNodes;
		for (var i = 0; i < tabs.length; ++i)
		{
			var tab = tabs[i];
			try
			{
				var browser = tabbrowser.getBrowserForTab(tab);
				if (browser)
				{
					var doc = browser.contentDocument;
					var loc = doc.location.toString();
					if (loc == url)
					{
						gBrowser.selectedTab = tab;
						return;
					}
				}
			}
			catch (e)
			{
			}
		}
    // There is no tab. open new tab...
    var tab = gBrowser.addTab(url, null, null);
    gBrowser.selectedTab = tab;
  },
***************************************************/

	icon_moveon : function ()
	{
		document.getElementById("firefive-withurl-update-button").hidden = false;
		document.getElementById('firefive-update').src = "chrome://firefive/skin/follow5_mouseon.png"
		var state_label = document.getElementById('firefive-count')
		
		state_label.hidden = false;

		var length = this.get_url_count();

		if (length > 140)
		{
			state_label.style.color = "red";
			state_label.value = "已超过 " + ( length - 140 ) + " 字。";
		}
		else
		{
			state_label.style.color = "green";
			state_label.value = "还可以输入 " + ( 140 - length ) + " 字。";
		}

	},

	icon_moveout : function ()
	{
		var image = document.getElementById('firefive-update');

		if (image.src.match(/follow5_mouseon\.png/))
		{
			image.src =	"chrome://firefive/skin/follow5.png"
		}

		document.getElementById('firefive-count').hidden = true;
		document.getElementById("firefive-withurl-update-button").hidden = true;

	},

	get_url_count : function ()
	{
		// 计算地址栏字串的长度
		var status = document.getElementById("urlbar").value;
		length = status.length;
		return length;
	},

	url_key_press : function (e)
	{
		// 在地址栏按键
		this.icon_moveon();
		// 指定 2 秒后还原，但光标闪一下就调用一次，先注释掉吧  -_-#...
		// firefive.trendTimer = setInterval(function () { firefive.icon_moveout(); clearInterval(firefive.trendTimer);}, 2000 );
	},

	onMenuItemCommand: function(e)
	{
		var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
																	.getService(Components.interfaces.nsIPromptService);
		promptService.alert(window, this.strings.getString("helloMessageTitle"),
																this.strings.getString("helloMessage"));
	},

	onToolbarButtonCommand: function(e)
	{
		// 重用上面的函数，当然也可以修改这里
		firefive.onMenuItemCommand(e);
	}
};

window.addEventListener("load", firefive.onLoad, false);
